const App = getApp()
Page({
  data: {
    addressDetail: '',
    latitude: '',
    longitude: '',
    images: '',
    imageid: '',
    forminfo: ''
  },
  onLoad: function (options) {

  },
  onReady: function () {

  },
  formSubmit(e) {
    let that = this
    if (e.detail.value.title == '' || e.detail.value.phone == '' || e.detail.value.detail == '') {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.latitude == '' || that.data.latitude == '') {
      wx.showToast({
        title: '请选择地址信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.imageid=='') {
      wx.showToast({
        title: '请上传维修站宣传广告图',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let data = {
      title: e.detail.value.title,
      service_type: 0,
      phone: e.detail.value.phone,
      detail: e.detail.value.detail,
      address: that.data.addressDetail,
      longitude: that.data.longitude,
      latitude: that.data.latitude,
      image_id: that.data.imageid
    }
    App.api.$http({
      url: 'service/submit',
      method: 'POST',
      data
    }, {}).then(res => {
      if (res) {
        App.api.$showSuccess(res.msg)
        that.setData({
          forminfo: '',
          addressDetail: '',
          images: ''
        })
        setTimeout(function () {
          wx.navigateBack()
        }, 2000)
      }
    })
  },
  onShow: function () {

  },

  chooseimage(e) {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths[0]
        wx.showLoading({
          title: '上传中',
        })
        wx.uploadFile({
          url: 'https://jiayou.ziming.work/index.php?s=/api/upload/image',
          filePath: tempFilePaths,
          name: 'iFile',
          header: {
            'content-type': 'multipart/form-data',
          },
          formData: {
            wxapp_id: '10005',
            token: wx.getStorageSync("user_token") || ''
          },
          success(res) {
            wx.hideLoading()
            var data = JSON.parse(res.data);
            that.setData({
              imageid: data.data.file_id,
              images: data.data.file_path
            })
          }
        })
      }
    })
  },

  choose() {
    let that = this
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          addressDetail: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        })
      },
      fail(res) {}
    })
  },
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})